﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Core
{
    public static partial class user
    {
        public static bool checkPassword(string username, string password)
        {
            var sql = new Sql.SqlDataContext();
            return sql.User.Any(row => row.username == username && row.password == password);
        }

        public static dynamic getUser(string username)
        {
            var sql = new Sql.SqlDataContext();
            var u = sql.User.First(row => row.username.ToLower() == username.ToLower());
            return new
            {
                u.username,
                u.nickname,
                u.email,
                u.motto,
                u.publicPersonalInfo,
                u.sid,
                u.name,
                sex = !u.sex.HasValue ? 0 : u.sex.Value ? 1 : 2,
                u.school,
                birthday = !u.birthday.HasValue ? null : u.birthday.Value.ToShortDateString(),
                u.admin,
                u.teacher
            };
        }

        public static dynamic doLogin(string username, string password)
        {
            var sql = new Sql.SqlDataContext();
            var u = sql.User.FirstOrDefault(row => row.username.ToLower() == username.ToLower() || row.sid == username);
            if (u != null) // Found local user
            {
                if (u.disabled) return new
                  {
                      success = false,
                      msg = "用户已停用"
                  };
                else if (u.password == password)
                {
                    u.lastLoginTime = DateTime.Now;
                    try { sql.SubmitChanges(); }
                    catch { }
                    return new
                    {
                        success = true,
                        student = (object)null,
                        username = u.username
                    };
                }
                else return new
               {
                   success = false,
                   msg = "用户不存在或密码错误"
               };
            }
            else // Verify remote user
            {
                var h = new EMIS.Helper();
                if (h.Login(username, password))
                {
                    var s = sql.Student.FirstOrDefault(row => row.sid == username);
                    if (s == null)
                    {
                        var inf = h.GetInfo();
                        s = new Sql.Student
                        {
                            sid = inf.sid,
                            Name = inf.Name,
                            Sex = inf.Sex,
                            College = inf.College,
                            Subject = inf.Subject,
                            Class = inf.Class,
                            Nation = inf.Nation,
                            Birthday = inf.Birthday,
                            BirthPlace = inf.BirthPlace,
                            IDCard = inf.IDCard,
                            Photo = inf.Photo
                        };
                        sql.Student.InsertOnSubmit(s);
                        sql.SubmitChanges();
                    }
                    return new
                    {
                        success = true,
                        student = new
                        {
                            s.sid,
                            name = s.Name,
                            school = "合肥工业大学",
                            sex = s.Sex.Value ? 1 : 2,
                            birthday = s.Birthday.Value.ToShortDateString(),
                        }
                    };
                }
                else return new
                {
                    success = false,
                    msg = "用户不存在或密码错误"
                };
            }
        }
    }
}
